package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.constans.Global;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.CashApply;
import com.wsd.smartcarwasher.domain.Partner;
import com.wsd.smartcarwasher.service.CashApplyService;
import com.wsd.smartcarwasher.service.PartnerService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Api(tags = "提现申请相关接口")
@RestController
@CrossOrigin
public class CashApplyController {

    @Resource
    private PartnerService partnerService;

    @Resource
    private WxService wxService;

    @Resource
    private CashApplyService cashApplyService;

    @ApiOperation(value="发起提现请求", notes = "运营端提现页", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "cashApply", value = "提现请求", required = true, dataType = "CashApply")
    @RequestMapping(value = "/v1/pick_profit", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap applyToCash(@RequestBody CashApply cashApply, HttpServletRequest request){
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        String openId = wxService.getOpenId(sessionId);
        if (cashApply.getPickProfit().compareTo(BigDecimal.ZERO) <= 0) {
            return ReturnUtil.Error(ResultState.CASH_APPLY_AMOUNT_IS_ZERO.getMsg());
        }
        Partner partner = partnerService.getPartnerWithCardNumByOpenId(openId);
        if (partner == null) {
            return ReturnUtil.Error(ResultState.NO_BIND_PARTNER.getMsg());
        }
        if(partner.getIs_dataer()==1)
        {
            return ReturnUtil.Error("没有权限");
        }
        return cashApplyService.applyToCash(partner, cashApply);
    }

    @ApiOperation(value="查询运营商提交的提现申请", notes = "运营端提现页", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/query_picking_records", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap partnerrPickProfits(HttpServletRequest request) {
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        String openId = wxService.getOpenId(sessionId);
        Partner partner = partnerService.getPartnerByOpenId(openId);
        if (partner == null) {
            return ReturnUtil.Error(ResultState.NO_BIND_PARTNER.getMsg());
        }
        if(partner.getIs_dataer()==1)
        {
            return ReturnUtil.Error("没有权限");
        }
        List<CashApply> cashApplyList = cashApplyService.partnerCashApplies(String.valueOf(partner.getId()));
        return ReturnUtil.Success(ResultState.QUERY_CASH_APPLY_SUCCESS.getMsg(), cashApplyList);
    }

    @ApiOperation(value="查询所有提现申请", notes = "管理端提现申请页", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/all_picking_records", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap allPickingRecords() {
        List<CashApply> cashApplyList = cashApplyService.allPickingRecords();
        return ReturnUtil.Success(ResultState.QUERY_CASH_APPLY_SUCCESS.getMsg(), cashApplyList);
    }

    @ApiOperation(value="处理提现申请", notes = "管理端提现申请页", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "cashApply", value = "提现请求", required = true, dataType = "CashApply")
    @RequestMapping(value = "/v1/handle_picking_profit", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap updateCashApply(@RequestBody CashApply cashApply) {
        return cashApplyService.updateCashApply(cashApply);
    }
}
